<?php
class SearchOption extends AppModel {

	var $name = 'SearchOption';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
	var $belongsTo = array(
			'User' => array('className' => 'User',
								'foreignKey' => 'user_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			)
	);
*/

	function getData($uid) {
		$c = array('user_id'=>$uid);
		$f = array();
		if (!$this->hasAny($c)) {
			$this->save($c);
		}
		return $this->find('first', array('conditons'=>$c, 'fields'=>$f));
	}

	function saveSearchOptions($data) {
		if (!empty($data['relations'])) {
			$data['relations'] = implode(';', $data['relations']);
		} else {
			$data['relations'] = '';
		}
		return $this->save($data);
	}

	function getSearchConditions($uid) {
		$data = $this->getData($uid);
		$data = $data[$this->name];
		$conditions = array();
		if (!empty($data['city'])) {
			$conditions['Profile.city'] = $data['city'];
		}

		if (!empty($data['agefrom'])) {
			$date = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d"),   date("Y")-$data['agefrom']));
			$conditions['Profile.birthday <='] = $date;
		}

		if (!empty($data['ageto'])) {
			$date = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d"),   date("Y")-$data['ageto']));
			$conditions['Profile.birthday >='] = $date;
		}

		return $conditions;
	}

	function getSearchOptionID($uid) {
		return $this->field('id', array('user_id'=>$uid));
	}

}
?>
